package com.whfc.openapi.api.face.controller;

import com.alibaba.fastjson.JSONObject;
import com.whfc.common.enums.RecMode;
import com.whfc.common.util.DateUtil;
import com.whfc.emp.enums.FaceGateType;
import com.whfc.emp.param.EmpAttendSyncDataParam;
import com.whfc.openapi.api.face.service.FaceGateSyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Description 沃土闸机回调
 * @Author hw
 * @Date 2021/1/20 16:57
 * @Version 1.0
 */
@RestController
@RequestMapping("/open/api/faceGate/wo")
public class FaceGateWoController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FaceGateSyncService faceGateSyncService;

    /**
     * 人员照片下发状态回调
     */
    @PostMapping("photoAuth/callback")
    public String photoAuthCallback(@RequestParam(value = "personGuid", required = false) String personGuid,
                                    @RequestParam(value = "faceGuid", required = false) String faceGuid,
                                    @RequestParam(value = "appId", required = false) String appId,
                                    @RequestParam(value = "deviceKey", required = false) String deviceKey,
                                    @RequestParam(value = "state", required = false) Integer state,
                                    @RequestParam(value = "msg", required = false) String msg,
                                    @RequestParam(value = "time", required = false) String time) {
        logger.info("wo,人员照片下发状态回调,personGuid:{},faceGuid:{},appId:{},deviceKey:{},state:{},msg:{},time:{}", personGuid, faceGuid, appId, deviceKey, state, msg, time);

        return "success";
    }

    /**
     * 人员识别回调
     */
    @PostMapping("rec/callback")
    public String recCallback(@RequestParam(value = "personGuid", required = false) String personGuid,
                              @RequestParam(value = "deviceKey", required = false) String deviceKey,
                              @RequestParam(value = "photoUrl", required = false) String photoUrl,
                              @RequestParam(value = "showTime", required = false) String showTime,
                              @RequestParam(value = "data", required = false) String data,
                              @RequestParam(value = "type", required = false) Integer type,
                              @RequestParam(value = "temperature", required = false) Double temperature,
                              @RequestParam(value = "idCardInfo", required = false) String idCardInfo) {
        logger.info("wo,人员识别回调,personGuid:{},deviceKey:{},photoUrl:{},showTime:{},data:{},type:{},temperature:{},idCardInfo:{}",
                personGuid, deviceKey, photoUrl, showTime, data, type, temperature, idCardInfo);
        EmpAttendSyncDataParam dataParam = new EmpAttendSyncDataParam();
        dataParam.setPersonGuid(personGuid);
        dataParam.setTemperature(temperature);
        dataParam.setDeviceKey(deviceKey);
        dataParam.setPicture(photoUrl);
        dataParam.setRecMode(RecMode.FACE.getValue());
        Date time = new Date();
        if (showTime != null) {
            time = DateUtil.parseDate(showTime, "yyyy-MM-dd'T'HH:mm:ssZ");
        }
        dataParam.setShowTime(time);
        dataParam.setPlatform(FaceGateType.WO.getCode());
        faceGateSyncService.syncAttend(dataParam);
        return "success";
    }

    /**
     * 人员识别回调
     */
    @PostMapping("rec/v2/callback")
    public String recCallback(@RequestParam(value = "eventMsg", required = false) String eventMsg) {
        logger.info("wo,人员识别回调v2,eventMsg={}", eventMsg);
        JSONObject jsonBody = JSONObject.parseObject(eventMsg);
        EmpAttendSyncDataParam dataParam = new EmpAttendSyncDataParam();
        dataParam.setPersonGuid(jsonBody.getString("admitGuid"));
        dataParam.setDeviceKey(jsonBody.getString("deviceNo"));
        dataParam.setPicture(jsonBody.getString("filePath"));
        Long showTime = jsonBody.getLong("showTime");
        Integer direction = jsonBody.getInteger("direction"); // 1进2出
        dataParam.setDirection(direction);
        if (null != direction && 2 == direction) {
            dataParam.setDirection(0);
        }
        dataParam.setRecMode(RecMode.FACE.getValue());
        Date time = new Date();
        if (showTime != null) {
            time = new Date(showTime);
        }
        dataParam.setShowTime(time);
        dataParam.setPlatform(FaceGateType.WO.getCode());
        faceGateSyncService.syncAttend(dataParam);
        return "success";
    }
}
